﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Pathology
namespace backoffice.Data
{
    partial class Pathology
    {
        public void setPathology(Pathology p)
        {
            this.PathologyID = p.PathologyID;
            this.Name = p.Name;
            this.Description = p.Description;
        }
    }
}
#endregion

#region dataPathology
namespace backoffice.Data
{
    public class dataPathology
    {
        #region Create
        public static void createPathology(Pathology p)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                p.PathologyID = Guid.NewGuid();

                db.Pathologies.InsertOnSubmit(p);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Pathology getPathology(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Pathologies.FirstOrDefault(p => p.PathologyID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Pathology> getPathologies(Expression<Func<Pathology, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Pathologies.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Pathology> getPathologies_Plants(Expression<Func<Plants_Pathology, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Pathologies.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changePathology(Pathology p)
        {
            try
            {
                if (p == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Pathology res = db.Pathologies.FirstOrDefault(pa => pa.PathologyID == p.PathologyID);
                    if (res == null) return false;
                    else
                    {
                        res.setPathology(p);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deletePathology(Pathology p)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Pathologies.Contains(p))
                {
                    Pathology res = db.Pathologies.FirstOrDefault(pa => pa.PathologyID == p.PathologyID);

                    if (res == null) return false;
                    else
                    {
                        // Update experts
                        IQueryable<Expert> experts = db.Experts.Where((Expert ex) => ex.PathologyID == res.PathologyID);

                        foreach (Expert e in experts)
                        {
                            e.PathologyID = null;
                        }

                        // Delete plants_patology
                        IQueryable<Plants_Pathology> pp = db.Plants_Pathologies.Where((Plants_Pathology p_p) => p_p.PathologyID == res.PathologyID);
                        db.Plants_Pathologies.DeleteAllOnSubmit(pp);

                        // Delete questions
                        IQueryable<Question> questions = db.Questions.Where((Question q) => q.PathologyID == res.PathologyID);
                        db.Questions.DeleteAllOnSubmit(questions);

                        // Delete pathology
                        db.Pathologies.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
#endregion
